// Copyright 2007 Google Inc.  All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.connector.gdata;

import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.spi.Value;
import com.google.enterprise.connector.spi.SpiConstants;
import com.google.enterprise.connector.spi.Document;
import com.google.enterprise.connector.spi.DocumentList;
import com.google.enterprise.connector.spi.SimpleDocument;
import com.google.enterprise.connector.spi.SimpleProperty;

import junit.framework.TestCase;

import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;

/**
 * This class is a test case that verifies several properties of the
 * GdDocumentList implementation, namely:
 * (1) The list uses the last modified property of the contained entries for
 * checkpointing, until after the last document, at which time it starts to use
 * the given final checkpoint string instead.
 * (2) All and only those documents passed to the constructor in a List are
 * returned identically, in order, by traversal. 
 * 
 * @author amsmith@google.com (Adam Smith)
 */
public class GdDocumentListTest extends TestCase {

  public void testLastmodfiedAndFinalCheckpointStringsUsedForCheckpointing()
  throws RepositoryException {
    LinkedList items = new LinkedList();
    items.add(makeDocument("a while ago"));
    items.add(makeDocument("just the other day"));
    
    DocumentList docList = new GdDocumentList(items, "right now");
    Document ignoredDocument = null;
    
    ignoredDocument = docList.nextDocument();
    assertEquals("a while ago", docList.checkpoint());
    ignoredDocument = docList.nextDocument();
    assertEquals("just the other day", docList.checkpoint());
    ignoredDocument = docList.nextDocument();
    assertEquals("right now", docList.checkpoint());
  }
  
  public void testTraversesDocumentsGivenToConstructor()
  throws RepositoryException {
    Document docA = makeDocument();
    Document docB = makeDocument();
    Document docC = makeDocument();
    
    LinkedList items = new LinkedList();
    items.add(docA);
    items.add(docB);
    items.add(docC);
    
    DocumentList docList = new GdDocumentList(items, "right now");
    
    assertSame(docA, docList.nextDocument());
    assertSame(docB, docList.nextDocument());
    assertSame(docC, docList.nextDocument());
    assertNull(null, docList.nextDocument());
  }
  
  private Document makeDocument() {
    return makeDocument(null);
  }
  
  private Document makeDocument(String lastModifiedString) {
    List valueList = new LinkedList();
    valueList.add(Value.getStringValue(lastModifiedString));
    Map propertyMap = new HashMap();
    propertyMap.put(SpiConstants.PROPNAME_LASTMODIFIED,
        new SimpleProperty(valueList));
    return new SimpleDocument(propertyMap); 
  }
}
